package com.ccit.zhxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.zhxy.pojo.Grade;
import com.ccit.zhxy.service.GradeService;
import com.ccit.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 鹏少
 * @Date 2023/12/26
 * @Date 14:08
 */
@RestController
@RequestMapping("/sms/gradeController")
@Api(tags = "年级控制器")
public class GradeController {

    @Autowired
    private GradeService gradeService;


    @ApiOperation("根据年级名称模糊查询，带条件的分页查询")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result page(
            @ApiParam("分页查询的页码数") @PathVariable Integer pageNo,
            @ApiParam("分页查询的页大小") @PathVariable Integer pageSize,
            @ApiParam("分页查询模糊匹配的名称") String gradeName) {
        Page<Grade> gradePage = new Page<>(pageNo, pageSize);
        Page<Grade> page = gradeService.lambdaQuery().like(gradeName != null, Grade::getName, gradeName)
                .orderByDesc(Grade::getId).orderByAsc(Grade::getName).page(gradePage);
        return Result.ok(page);

    }

    @ApiOperation("保存或新增修改Grade,有id为修改，没有则是增加")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@ApiParam("Json格式的Grade对象") @RequestBody Grade grade) {

        gradeService.saveOrUpdate(grade);
        return Result.ok();


    }

    @ApiOperation("删除Grade信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(@ApiParam("要删除的所有的Grade的id的json集合") @RequestBody List<Integer> ids) {

        gradeService.removeByIds(ids);
        return Result.ok();

    }

    @ApiOperation("查询出所有的年级")
    @GetMapping("/getGrades")
    public Result getGrades(){

        List<Grade> list = gradeService.lambdaQuery().list();
        return Result.ok(list);

    }


}
